package com.tools.common.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * @author Admin
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{

    /**
     * 将下划线转为小驼峰
     *
     * @param str 字符串
     * @param b   true 首字母小写 false 首字母大写
     * @return
     */
    public static String underlineToHumpSmall(String str, boolean b) {
        str = str.toLowerCase();
        Pattern compile = Pattern.compile("_[a-z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
        }
        matcher.appendTail(sb);
        if (!b) {
            return convertToCamelCase(sb.toString());
        }
        return sb.toString();
    }

    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串。 例如：HELLO_WORLD->HelloWorld
     *
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    public static String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty()) {
            return "";
        } else if (!name.contains("_")) {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (String camel : camels) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty()) {
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }

    /***
     * 将驼峰转为下划线
     * @param str
     * @return
     */
    public static String humpToUnderline(String str) {
        Pattern compile = Pattern.compile("[A-Z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /***
     * 将下划线转为驼峰 大驼峰
     * @param str
     * @return
     */
    public static String underlineToHump(String str) {
        str = str.toLowerCase();
        Pattern compile = Pattern.compile("_[a-z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
        }
        String str_ = matcher.appendTail(sb).toString();
        String aCase = convertToCamelCase(str_);
        return aCase;
    }

    public static String queryIn(List<String> tabs) {
        if (tabs.isEmpty()){
            return "";
        }
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < tabs.size(); i++) {
            buf.append("\'");
            buf.append(tabs.get(i));
            buf.append("\'");
            if (i != tabs.size() - 1 ) {
                buf.append(",");
            }
        }
        return buf.toString();
    }

    public static String queryIn(String... element) {
        if (element.length == 0) {
            return "";
        }
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < element.length; i++) {
            buf.append("\'");
            buf.append(element[i]);
            buf.append("\'");
            if (i != element.length - 1 ) {
                buf.append(",");
            }
        }
        return buf.toString();
    }

    /**
     * 如果为null 返回空串
     *
     * @param str
     * @return
     */
    public static String getEmptyStr(String str) {
        if (null == str || str.isEmpty()){
            return "";
        }
        return str;
    }

    /**
     * 如果为空 返回空字符串 否则返回toString
     * @param obj 待判断的字符串
     * @return 字符串
     */
    public static String parseStr(Object obj) {
        if (null == obj || "".equals(obj.toString())){
            return "";
        }
        return obj.toString();
    }

    /**
     * 下划线转驼峰
     * @param fieldName
     * @return
     */
    public static String underlineToCamel(String fieldName) {
        String temp = fieldName.toLowerCase();
        int len = temp.length();
        StringBuilder sb = new StringBuilder(len);

        for(int i = 0; i < len; ++i) {
            char c = temp.charAt(i);
            if (c == '_') {
                ++i;
                if (i < len) {
                    sb.append(Character.toUpperCase(temp.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);

        for(int i = 0; i < len; ++i) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c) && i > 0) {
                sb.append('_');
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 首字母大写
     * @param param
     * @return
     */
    public static String firstToLowerCase(String param) {
        return isBlank(param) ? "" : param.substring(0, 1).toLowerCase() + param.substring(1);
    }

}
